package com.zyh.system.supermark1.service.impl;

import com.zyh.system.supermark1.controller.BuyIndex;
import com.zyh.system.supermark1.dao.impl.UserDaoImpl;
import com.zyh.system.supermark1.domain.User;
import com.zyh.system.supermark1.service.UserService;

import java.util.List;
import java.util.Scanner;

public class UserServiceImpl implements UserService {

    public static final Scanner INPUT = new Scanner(System.in);
    public static final UserDaoImpl USER_DAO = new UserDaoImpl();
    public static final BuyIndex BUY_INDEX = new BuyIndex();

    /**
     * 注册功能
     */
    @Override
    public void register() {
        System.out.println("请输入用户名：");
        String userName = INPUT.next();
        //判断输入的用户名是否为空
        if (userName == null || userName.length() == 0 || userName.isEmpty() == true) {
            System.out.println("用户名不能为空");
            return;
        }
        //判断用户名是否已经存在
        User regUser = USER_DAO.find(userName);
        if (regUser!=null){
            System.out.println("用户已经存在！");
            return;
        }
        outer:
        while (true) {
            System.out.println("请输入密码");
            String password = INPUT.next();
            while (password.length() < 6) {
                System.out.println("密码长度不能小于6");
                System.out.println("请重新输入");
                password = INPUT.next();
            }
            System.out.println("请再次输入密码，确认密码");
            String password2 = INPUT.next();
            if (!(password.equals(password2))) {
                System.out.println("两次输入的密码不一致");
                continue outer;
            } else {
                System.out.println("请输入年龄：");
                Integer age = INPUT.nextInt();
                while (age < 0 || age > 100) {
                    System.out.println("您的年龄必须是0-100之间的数字");
                    System.out.println("请重新输入年龄：");
                    age = INPUT.nextInt();
                }
                System.out.println("请输入昵称：");
                String otherName = INPUT.next();
                User user = new User(null, userName, password, otherName, age);
                int count = USER_DAO.register(user);
                if (count > 0) {
                    System.out.println("恭喜您：" + userName + "注册成功！");
                } else {
                    System.out.println("注册失败");
                }
                return;
            }
        }
    }

    /**
     * 登录功能
     */
    public void login() {
        User user =null;
        while (true) {
            System.out.println("请输入用户名：");
            String name = INPUT.next();
            if (name == null || name.length() == 0 || "".equals(name)) {
                System.out.println("用户名不能为空！");
                continue;
            }
            System.out.println("请输入密码：");
            String psd = INPUT.next();
            user = new User(null, name, psd, null, null);
            List<User> resultList = USER_DAO.login(user);
            if (resultList.size()==0) {
                System.out.println("用户名或者密码不正确！");
                continue;
            } else {
                System.out.println("登陆成功\t欢迎您：" + resultList.get(0).getUserName());
                user=resultList.get(0);
                break;
            }
        }
        BUY_INDEX.buyIndex(user);
    }
}
